package products

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/brand"
	"github.com/flipped-aurora/gin-vue-admin/server/model/classey"
	"github.com/flipped-aurora/gin-vue-admin/server/model/products"
	productsReq "github.com/flipped-aurora/gin-vue-admin/server/model/products/request"
)

type ProductService struct{}

// CreateProduct 创建商品管理记录
// Author [yourname](https://github.com/yourname)
func (productService *ProductService) CreateProduct(ctx context.Context, product *products.Product) (err error) {
	if product.Name == nil || *product.Name == "" {
		return fmt.Errorf("商品名不得为空")
	}
	if product.Price == nil || *product.Price <= 0 {
		return fmt.Errorf("商品价格不得小于等于0")
	}
	err = global.GVA_DB.Create(product).Error
	return err
}

// DeleteProduct 删除商品管理记录
// Author [yourname](https://github.com/yourname)
func (productService *ProductService) DeleteProduct(ctx context.Context, productId string) (err error) {
	err = global.GVA_DB.Model(&products.Product{}).Where("product_id = ?", productId).Update("status", "0").Error
	return err
}

// DeleteProductByIds 批量删除商品管理记录
// Author [yourname](https://github.com/yourname)
func (productService *ProductService) DeleteProductByIds(ctx context.Context, productIds []string) (err error) {
	err = global.GVA_DB.Model(&products.Product{}).Where("product_id IN (?)", productIds).Update("status", "0").Error
	return err
}

// UpdateProduct 更新商品管理记录
// Author [yourname](https://github.com/yourname)
func (productService *ProductService) UpdateProduct(ctx context.Context, product products.Product) (err error) {
	err = global.GVA_DB.Model(&products.Product{}).Where("product_id = ?", product.ProductId).Updates(&product).Error
	data, _ := json.Marshal(&product)
	//request := esapi.IndexRequest{
	//	Index:      "products",
	//	DocumentID: fmt.Sprintf("%d", product.ProductId),
	//	Body:       strings.NewReader(string(data)),
	//}
	//_, err = request.Do(ctx, global.ES)
	key := fmt.Sprintf("product:%d", *product.ProductId)
	global.Redis.Set(context.Background(), key, string(data), 0)
	return err
}

// GetProduct 根据productId获取商品管理记录
// Author [yourname](https://github.com/yourname)
func (productService *ProductService) GetProduct(ctx context.Context, productId string) (product products.Product, err error) {
	err = global.GVA_DB.Where("product_id = ?", productId).First(&product).Error
	return
}

// GetProductInfoList 分页获取商品管理记录
// Author [yourname](https://github.com/yourname)
func (productService *ProductService) GetProductInfoList(ctx context.Context, info productsReq.ProductSearch) (list []products.ProductData, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&products.Product{})

	var products []products.ProductData
	// 如果有条件搜索 下方会自动创建搜索语句
	db = db.Joins("join brand on product.brand_id = brand.brand_id")
	db = db.Joins("join category on product.category_id = category.category_id")
	if info.BrandId != nil {
		db = db.Where("product.brand_id = ?", *info.BrandId)
	}
	if info.CategoryId != nil {
		db = db.Where("product.category_id = ?", *info.CategoryId)
	}
	if info.Name != nil && *info.Name != "" {
		db = db.Where("product.name LIKE ?", "%"+*info.Name+"%")
	}
	if info.Status != nil && *info.Status != "" {
		db = db.Where("product.status = ?", *info.Status)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	db = db.Select("product.*,brand.brand_name as brand,category.category_name as category")
	err = db.Find(&products).Error
	return products, total, err
}
func (productService *ProductService) GetProductPublic(ctx context.Context) ([]classey.Category, []brand.Brand) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
	var Classes []classey.Category
	var Brands []brand.Brand
	global.GVA_DB.Find(&Classes)
	global.GVA_DB.Find(&Brands)
	return Classes, Brands
}
